﻿using System;
using System.Linq;
using SkiaSharp;

namespace Solution.Comm.Code
{
    public class SkiaSharpHelper
    {

        public string RndNum(int VcodeNum)
        {
            //验证码可以显示的字符集合  
            string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p" +
                          ",q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,P,Q" +
                          ",q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,P,Q" +
                    ",R,S,T,U,V,W,X,Y,Z";
            string[] VcArray = Vchar.Split(new Char[] { ',' });//拆分成数组   
            string code = "";//产生的随机数  
            int temp = -1;//记录上次随机数值，尽量避避免生产几个一样的随机数  

            Random rand = new Random();
            //采用一个简单的算法以保证生成随机数的不同  
            for (int i = 1; i < VcodeNum + 1; i++) {
                if (temp != -1) {
                    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));//初始化随机类  
                }
                int t = rand.Next(61);//获取随机数  
                if (temp != -1 && temp == t) {
                    return RndNum(VcodeNum);//如果获取的随机数重复，则递归调用  
                }
                temp = t;//把本次产生的随机数记录起来  
                code += VcArray[t];//随机数的位数加一  
            }
            return code;
        }

        public SKBitmap Code(string CodeText)
        {
            //string CodeText = "3bm7"; //实际的项目中不能写死

            var charList = CodeText.ToList();

            SKBitmap bmp = new SKBitmap(80, 30);

            using (SKCanvas canvas = new SKCanvas(bmp)) {

                //背景色

                canvas.DrawColor(SKColors.White);



                using (SKPaint sKPaint = new SKPaint()) {

                    sKPaint.TextSize = 16; //字体大小

                    sKPaint.IsAntialias = true; //开启抗锯齿                  

                    sKPaint.Typeface = SKTypeface.FromFamilyName("微软雅黑");

                    SKRect size = new SKRect();

                    //计算文字宽度以及高度

                    sKPaint.MeasureText(charList[0].ToString(), ref size);

                    float temp = (bmp.Width / 4 - size.Size.Width) / 2;

                    float temp1 = bmp.Height - (bmp.Height - size.Size.Height) / 2;

                    Random random = new Random();



                    //画文字

                    for (int i = 0; i < 4; i++) {

                        sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255),
                            (byte)random.Next(0, 255));

                        canvas.DrawText(charList[i].ToString(), temp + 20 * i, temp1, sKPaint);

                    }

                    //画干扰线

                    for (int i = 0; i < 5; i++) {

                        sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255),
                            (byte)random.Next(0, 255));

                        canvas.DrawLine(random.Next(0, 40), random.Next(1, 29), random.Next(41, 80), random.Next(1, 29),
                            sKPaint);

                    }

                }

            }


            return bmp;
        }
    }
}